<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 输入框 -->
    <TodoHeader @add="fn" />

    <!-- 列表区域 -->
    <TodoMain :list="list" @delByIndex="list.splice($event, 1)" />

    <!-- 统计和清空 -->
    <TodoFooter />
  </section>
</template>

<script>
import TodoFooter from './components/TodoFooter.vue';
import TodoMain from './components/TodoMain.vue'
import TodoHeader from './components/TodoHeader.vue'
import EventBus from './utils/EventBus.js'
export default {
  created() {
    EventBus.$on('add', (val) => {
      this.list.unshift(
        {
          id: Date.now(),
          title: val,
          isDone: false
        }
      );
    })
  },
  data() {
    return {
      // list: [
      //   { id: 1, title: '吃饭饭', isDone: false },
      //   { id: 2, title: '睡觉觉', isDone: false },
      //   { id: 3, title: '打豆豆', isDone: false }
      // ] 
      //本地存储数据 
      list: JSON.parse(localStorage.getItem('list')) ? JSON.parse(localStorage.getItem('list')) : []
    };
  },
  watch: {
    list(newList) {
      localStorage.setItem('list', JSON.stringify(newList));
    }

  },
  components: { TodoHeader, TodoMain, TodoFooter },
  methods: {
    fn(task) {
      this.list.unshift(
        {
          id: Date.now(),
          title: task,
          isDone: false
        }
      );
    }
  }
}
</script>

<style></style>
